package cur.tool.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cur.nature.framework.core.log.LOG;
import cur.nature.framework.core.util.StringUtil;
import cur.nature.framework.mvc.crud.CrudController;
import cur.tool.service.Admin_DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用字典表 Controller
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-09 16:14:19
 */
@Tag(name = "校验码 接口")
@RestController
@RequestMapping("tool/v1/api/Captcha")
public class Tool_CaptchaController extends CrudController<Admin_DictService> {
    public Tool_CaptchaController(Admin_DictService baseService) {
        super(baseService);
    }

    private static final String CAPTCHA_KEY = "captcha";

    @Operation(summary = "下载校验码")
    @GetMapping("/img")
    public void img(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ServletOutputStream servletOutputStream = null;
        try {
            // 定义图形验证码的长和宽(配置默认值)
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
            final String code = lineCaptcha.getCode();
            LOG.info("generate code: " + lineCaptcha.getCode());
            request.getSession().setAttribute(CAPTCHA_KEY, lineCaptcha.getCode());
            servletOutputStream = response.getOutputStream();
            lineCaptcha.write(servletOutputStream);

            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");
            LOG.debug("Tring to output buffered image to servlet output stream");
            servletOutputStream.flush();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
        finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
            }
        }
    }

    @Operation(summary = "验证校验码")
    @GetMapping("/check")
    public Map<String, String> check(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> result = new HashMap<>();
        final String captcha = request.getParameter(CAPTCHA_KEY);
        if (captcha == null) {
            result.put(CAPTCHA_KEY, "-1");
            return result;
        }
        final String localCaptcha = (String) request.getSession().getAttribute(CAPTCHA_KEY);
        if (StringUtil.equalsIgnoreCase(captcha, localCaptcha)) {
            result.put(CAPTCHA_KEY, "1");
            request.getSession().removeAttribute(CAPTCHA_KEY);
        } else {
            result.put(CAPTCHA_KEY, "0");
        }
        return result;
    }


}

